package com.adlotto2.lcdns.activity.ad;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.LottoAdApplication;
import com.adlotto2.lcdns.activity.ad.adapter.LottoAdListAdapter;
import com.adlotto2.lcdns.activity.info.LottoAdAdressListActivity;
import com.adlotto2.lcdns.common.BaseActivity;
import com.adlotto2.lcdns.cronos.data.CNLottoAdListData;
import com.adlotto2.lcdns.cronos.datahandler.CNBaseDataParser;
import com.adlotto2.lcdns.cronos.datahandler.CNBaseDataSet;
import com.adlotto2.lcdns.cronos.datahandler.CNResultSet;
import com.adlotto2.lcdns.cronos.http.CNLottoAdApiSet;
import com.adlotto2.lcdns.cronos.http.CNLottoAdApiSetEx;
import com.adlotto2.lcdns.cronos.parser.CNAdAllListParser;
import com.adlotto2.lcdns.util.ApplicationUtils;
import com.adlotto2.lcdns.util.CNCronosLog;
import com.adlotto2.lcdns.util.LottoAdImageDownloaderFile;
import com.adlotto2.lcdns.util.LottoAdOverButton;
import com.adlotto2.lcdns.util.UnbindHelper;

public class LottoAdAllAdListActivity extends BaseActivity{
	private Context mContext;
	
	private LottoAdApplication mApplication;
	
	/** 목록 없을시의 텍스트뷰 **/
	private TextView mEmptyListTv;
	/** 광고 목록 AdapterView **/
	private LottoAdListAdapter mListAdapter;
	/** 광고 페이징 레이아웃(이전,이후 버튼 & Page Number) **/
	private LinearLayout mPageLayout;
	
	/** 전화 걸시 인덱스 **/
	private final int INDEX_REQUEST_CALL = 1;
	
	/** 현재 액티비티 체크 인덱스값 **/
	private int mCurrentIndex = 0;
	/** 광고 시퀀스 **/
	private int mAdSeq = 0;
	
	/** 데이터 셋 **/
	private ArrayList<CNBaseDataSet> mDataSetList;
	
	private ArrayList<String> mOriginImageArr;
	private ArrayList<String> mPhoneNumberArr;
	
	/** 이전 버튼 **/
	private Button mPrevBtn;
	/** 다음 버튼 **/
	private Button mNextBtn;
	/** 광고 페이징 카운트 텍스트뷰 **/
	private TextView mCountTv;
		
	/** 목록의 전체 페이지 수 **/
	private int mTotalPage = 0;
	
	// 현재 어떤 리스트 요청이 와있는지 정의함. 0은 기본요청.
	private int mCurrentListOption = 0;
	
	private final int OPTION_NORMAL = 0;
	private final int OPTION_PART = 1;
	private final int OPTION_JOB = 2;
	private final int OPTION_LOCATION = 3;
	
	// 지역, 업종, 위치에 따른 데이터 소팅용 버튼
	private Button mPartButton;
	private Button mJobButton;
	private Button mLocationButton;
	
	private String mAddress, mSido, mGugun, mDong;
	private String mJob, mJobCode;
	
	/** 요청 페이지 Number **/
	private int mPage = 1;
	/** 한페이지당 요청 Item 갯수 **/
	private final int PAGE_SIZE = 8;
	
	// 3초 페이지 넘버 보기용 테스크
	private AsyncTask<Void, Void, Void> mTimerTask;
	
	// 리스트 목록 이미지 뷰
	private ImageView mImgView1, mImgView2, mImgView3, mImgView4, mImgView5, mImgView6, mImgView7, mImgView8;
	private View mInc1, mInc2, mInc3, mInc4, mInc5, mInc6, mInc7, mInc8;
	private ProgressBar mProgress1, mProgress2, mProgress3, mProgress4, mProgress5, mProgress6, mProgress7, mProgress8;
	private LinearLayout mListLayout;
	private LottoAdImageDownloaderFile mImageDownloader;
	
	
	@Override
	protected int onGetContentViewResource() {
		return R.layout.lottoad_menu_ad_list;
	}

	@Override
	protected void onInit() {
		buildComponnent();
		mApplication = (LottoAdApplication) getApplicationContext();
		initViews();
	}
	
	
	/*********************************************************************************************/
	/*********************************************************************************************/
	/*********************************************************************************************/
	
	public void initViews() {
		mImageDownloader = new LottoAdImageDownloaderFile(this);
		
		mListLayout = (LinearLayout) findViewById(R.id.ll_allad_lotto);
		
		mInc1 = findViewById(R.id.include_all_list1);
		mInc2 = findViewById(R.id.include_all_list2);
		mInc3 = findViewById(R.id.include_all_list3);
		mInc4 = findViewById(R.id.include_all_list4);
		mInc5 = findViewById(R.id.include_all_list5);
		mInc6 = findViewById(R.id.include_all_list6);
		mInc7 = findViewById(R.id.include_all_list7);
		mInc8 = findViewById(R.id.include_all_list8);
		
		setListVisibility(0);
		
		mImgView1 = (ImageView) mInc1.findViewById(R.id.imageView_lottoAd);
		mImgView2 = (ImageView) mInc2.findViewById(R.id.imageView_lottoAd);
		mImgView3 = (ImageView) mInc3.findViewById(R.id.imageView_lottoAd);
		mImgView4 = (ImageView) mInc4.findViewById(R.id.imageView_lottoAd);
		mImgView5 = (ImageView) mInc5.findViewById(R.id.imageView_lottoAd);
		mImgView6 = (ImageView) mInc6.findViewById(R.id.imageView_lottoAd);
		mImgView7 = (ImageView) mInc7.findViewById(R.id.imageView_lottoAd);
		mImgView8 = (ImageView) mInc8.findViewById(R.id.imageView_lottoAd);
		
		mProgress1 = (ProgressBar) mInc1.findViewById(R.id.ProgressBarLayout);
		mProgress2 = (ProgressBar) mInc2.findViewById(R.id.ProgressBarLayout);
		mProgress3 = (ProgressBar) mInc3.findViewById(R.id.ProgressBarLayout);
		mProgress4 = (ProgressBar) mInc4.findViewById(R.id.ProgressBarLayout);
		mProgress5 = (ProgressBar) mInc5.findViewById(R.id.ProgressBarLayout);
		mProgress6 = (ProgressBar) mInc6.findViewById(R.id.ProgressBarLayout);
		mProgress7 = (ProgressBar) mInc7.findViewById(R.id.ProgressBarLayout);
		mProgress8 = (ProgressBar) mInc8.findViewById(R.id.ProgressBarLayout);
		
		mImgView1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(0);
			}
		});
		
		mImgView2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(1);
			}
		});
		
		mImgView3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(2);
			}
		});
		
		mImgView4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(3);
			}
		});
		
		mImgView5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(4);
			}
		});
		
		mImgView6.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(5);
			}
		});
		
		mImgView7.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(6);
			}
		});
		
		mImgView8.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageClickAction(7);
			}
		});
	}
	
	public void setListVisibility(int count) {
		switch(count) {
			case 0:
				mInc1.setVisibility(View.INVISIBLE);
				mInc2.setVisibility(View.INVISIBLE);
				mInc3.setVisibility(View.INVISIBLE);
				mInc4.setVisibility(View.INVISIBLE);
				mInc5.setVisibility(View.INVISIBLE);
				mInc6.setVisibility(View.INVISIBLE);
				mInc7.setVisibility(View.INVISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
			case 1:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.INVISIBLE);
				mInc3.setVisibility(View.INVISIBLE);
				mInc4.setVisibility(View.INVISIBLE);
				mInc5.setVisibility(View.INVISIBLE);
				mInc6.setVisibility(View.INVISIBLE);
				mInc7.setVisibility(View.INVISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
				
			case 2:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.VISIBLE);
				mInc3.setVisibility(View.INVISIBLE);
				mInc4.setVisibility(View.INVISIBLE);
				mInc5.setVisibility(View.INVISIBLE);
				mInc6.setVisibility(View.INVISIBLE);
				mInc7.setVisibility(View.INVISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
				
			case 3:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.VISIBLE);
				mInc3.setVisibility(View.VISIBLE);
				mInc4.setVisibility(View.INVISIBLE);
				mInc5.setVisibility(View.INVISIBLE);
				mInc6.setVisibility(View.INVISIBLE);
				mInc7.setVisibility(View.INVISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
				
			case 4:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.VISIBLE);
				mInc3.setVisibility(View.VISIBLE);
				mInc4.setVisibility(View.VISIBLE);
				mInc5.setVisibility(View.INVISIBLE);
				mInc6.setVisibility(View.INVISIBLE);
				mInc7.setVisibility(View.INVISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
				
			case 5:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.VISIBLE);
				mInc3.setVisibility(View.VISIBLE);
				mInc4.setVisibility(View.VISIBLE);
				mInc5.setVisibility(View.VISIBLE);
				mInc6.setVisibility(View.INVISIBLE);
				mInc7.setVisibility(View.INVISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
				
			case 6:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.VISIBLE);
				mInc3.setVisibility(View.VISIBLE);
				mInc4.setVisibility(View.VISIBLE);
				mInc5.setVisibility(View.VISIBLE);
				mInc6.setVisibility(View.VISIBLE);
				mInc7.setVisibility(View.INVISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
				
			case 7:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.VISIBLE);
				mInc3.setVisibility(View.VISIBLE);
				mInc4.setVisibility(View.VISIBLE);
				mInc5.setVisibility(View.VISIBLE);
				mInc6.setVisibility(View.VISIBLE);
				mInc7.setVisibility(View.VISIBLE);
				mInc8.setVisibility(View.INVISIBLE);
				break;
				
			case 8:
				mInc1.setVisibility(View.VISIBLE);
				mInc2.setVisibility(View.VISIBLE);
				mInc3.setVisibility(View.VISIBLE);
				mInc4.setVisibility(View.VISIBLE);
				mInc5.setVisibility(View.VISIBLE);
				mInc6.setVisibility(View.VISIBLE);
				mInc7.setVisibility(View.VISIBLE);
				mInc8.setVisibility(View.VISIBLE);
				break;
		}
	}
	
	public void setListImage() {
		int count = mDataSetList.size();
		
		for(int i = 0; i < count; i++) {
			CNLottoAdListData dataSet = (CNLottoAdListData) mDataSetList.get(i);
			ImageView imgView = null;
			ProgressBar progressBar = null;
			
			switch(i) {
				case 0:
					imgView = mImgView1;
					progressBar = mProgress1;
					break;
				case 1:
					imgView = mImgView2;
					progressBar = mProgress2;
					break;
				case 2:
					imgView = mImgView3;
					progressBar = mProgress3;
					break;
				case 3:
					imgView = mImgView4;
					progressBar = mProgress4;
					break;
				case 4:
					imgView = mImgView5;
					progressBar = mProgress5;
					break;
				case 5:
					imgView = mImgView6;
					progressBar = mProgress6;
					break;
				case 6:
					imgView = mImgView7;
					progressBar = mProgress7;
					break;
				case 7:
					imgView = mImgView8;
					progressBar = mProgress8;
					break;
			}
//			imgView.setImageResource(Color.WHITE);
			progressBar.setVisibility(View.VISIBLE);
			
			mImageDownloader.download(dataSet.adThumb, imgView, progressBar);
		}
		
	}
	
	public void imageClickAction(int which) {
		
		CNLottoAdListData data = (CNLottoAdListData) mDataSetList.get(which);
		
		
		/*####### Ben ########*/
		// 디테일 이미지 부분 새로만듦.
		Bundle extras = new Bundle();
		Intent intent = new Intent(mContext, LottoAdDetailActivity.class);
		extras.putString("originImage", data.adOrigin);
		extras.putString("adPhoneNumber", data.adPhoneNum);
		intent.putExtras(extras);
		startActivity(intent);
	}
	
	/*********************************************************************************************/
	/*********************************************************************************************/
	/*********************************************************************************************/
	
	private void buildComponnent(){
		this.mContext = this;
		
		mEmptyListTv = (TextView) findViewById(R.id.list_empty);
		mPageLayout = (LinearLayout) findViewById(R.id.layout_pageNum);
		
		mPrevBtn = (Button) findViewById(R.id.button_prev);
		mNextBtn = (Button) findViewById(R.id.button_next);
		mCountTv = (TextView) findViewById(R.id.textView_pageNum);
		mPartButton = (Button) findViewById(R.id.btn_adlist_sort_part);
		mJobButton = (Button) findViewById(R.id.btn_adlist_sort_job);
		mLocationButton = (Button) findViewById(R.id.btn_adlist_sort_location);
		
		mListAdapter = new LottoAdListAdapter(mContext);
		
//		mSearchBtn.setOnTouchListener(new View.OnTouchListener() {
//			
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				
//				InputMethodManager imm = (InputMethodManager)
//				mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
//				imm.hideSoftInputFromWindow(mSearchEt.getWindowToken(), 0);
//				
//				Drawable getBackground = v.getBackground();
//				if(event.getAction() == MotionEvent.ACTION_DOWN){
//					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
//				}
//				else if(event.getAction() == MotionEvent.ACTION_UP){
//					
//					getBackground.clearColorFilter();
//					v.setBackgroundDrawable(getBackground);
//					
//					mPage = 1;
//					mTotalPage = 0;
//					onRequestData();
//				}
//				return false;
//			}
//		});
		
		mPrevBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					if(mPage > 1){ // 페이지 숫자가 1보다 클경우 
						mPage--;
						onRequestData();
					} else {
						ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.popup_alert), "첫번째 페이지 입니다.");
					}
				}
				return false;
			}
		});
		
		mNextBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					if(mPage < mTotalPage){ // 현재페이지보다 토탈페이지가 클 경우
						mPage++;
						onRequestData();
					} else {
						ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.popup_alert), "마지막 페이지 입니다.");
					}
				}
				return false;
			}
		});
		
		mPartButton.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					setButtonSelectedBackground(OPTION_PART);
					
					showPartChoiceDialog();
				}
				return false;
			}
		});
		
		mJobButton.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					setButtonSelectedBackground(OPTION_JOB);
					startActivityForResult(new Intent(mContext, LottoAdJobChoiceActivity.class), OPTION_JOB);
				}
				return false;
			}
		});
		
		mLocationButton.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					setButtonSelectedBackground(OPTION_LOCATION);
					
					
					/*####### Ben ########*/
					// 아직 정의되지 않아서 현재 전체보기로 설정함.
					mPage = 1;
					mTotalPage = 0;
					mCurrentListOption = OPTION_LOCATION;
					onRequestData();
					
				}
				return false;
			}
		});
		
	}
	
	@Override
	public void onBackPressed() {
		if(mCurrentListOption == 0) {
			super.onBackPressed();
		} else {
			setButtonSelectedBackground(OPTION_NORMAL);
			mCurrentListOption = 0;
			mPage = 1;
			mTotalPage = 0;
			onRequestData();
		}
	}
	
	public void showPartChoiceDialog() {
		AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
		listDialog.setTitle(R.string.string_choice);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, getResources().getStringArray(R.array.part_oprion_array));
		listDialog.setAdapter(adapter, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				/* 0 : 시도, 1 : 시도/구군 , 2 : 시도/구군/읍면동 */
				Intent intent = new Intent(mContext, LottoAdAdressListActivity.class);
				intent.putExtra("isRegistActivity",	"false");
				intent.putExtra("searchLimit", which);
				startActivityForResult(intent, OPTION_PART);
			}
		});
		
		listDialog.show();
	}
	
	/*####### Ben ########*/
	// 버튼 선택이미지 변경.
	private void setButtonSelectedBackground(int which) {
		switch(which) {
			case OPTION_NORMAL:
				mPartButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_part_off));
				mLocationButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_location_off));
				mJobButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_job_off));
				break;
			case OPTION_PART:
				mPartButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_part_on));
				mLocationButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_location_off));
				mJobButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_job_off));
				break;
			case OPTION_LOCATION:
				mPartButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_part_off));
				mLocationButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_location_on));
				mJobButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_job_off));
				break;
			case OPTION_JOB:
				mPartButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_part_off));
				mLocationButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_location_off));
				mJobButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.bt_job_on));
				break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		mTotalPage = 0;
		mPage = 1;
		if(requestCode == OPTION_PART && resultCode == RESULT_OK){
			mCurrentListOption = OPTION_PART;
			mAddress = data.getStringExtra("address");
			mSido = data.getStringExtra("sido");
			mGugun = data.getStringExtra("gugun");
			mDong = data.getStringExtra("dong");
			if(!CNLottoAdApiSet.isLive()) {
				Log.i("BenTag", "sido : " + mSido + ", gugun : " + mGugun + ", dong : " + mDong);
			}
			
			if(mGugun == null) {
				mGugun = "";
				mDong = "";
			} else if(mDong == null) {
				mDong = "";
			}
			
		} else if(requestCode == OPTION_JOB && resultCode == RESULT_OK) {
			mCurrentListOption = OPTION_JOB;
			mJob = data.getStringExtra("job");
			mJobCode = data.getStringExtra("jobCode");
			if(!CNLottoAdApiSet.isLive()) {
				Log.e("BenTag", "Job : " + mJob + ", JobCode : " + mJobCode);
			}
			
		}
		
	}
	
	public void showPagenumDuringGivenTime(final long time) {
		
		mCountTv.setVisibility(View.GONE);
		
		if(mTimerTask != null) {
			if(mTimerTask.getStatus() == AsyncTask.Status.PENDING || mTimerTask.getStatus() == AsyncTask.Status.RUNNING) {
				mTimerTask.cancel(true);
			}
		}
		mTimerTask = null;
		
		mTimerTask = new AsyncTask<Void, Void, Void>() {
			
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				mCountTv.setVisibility(View.VISIBLE);
			}
			
			@Override
			protected Void doInBackground(Void... params) {
				try {
					Thread.sleep(time);
				} catch(InterruptedException e) {
					e.printStackTrace();
				}
				return null;
				
			}
			
			@Override
			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
				mCountTv.setVisibility(View.GONE);
			}
			
		}.execute();
		
		
	}
	
	private void parsingAdList(Element xmlObject) throws Exception{
		
		mDataSetList = new ArrayList<CNBaseDataSet>();
		mOriginImageArr = new ArrayList<String>();
		mPhoneNumberArr = new ArrayList<String>();
		
		CNBaseDataParser parser = new CNAdAllListParser();
		CNResultSet resultSet = parser.parse(xmlObject);
		ArrayList<CNBaseDataSet> dataSet = null;
		if (resultSet != null)
		{
			dataSet = resultSet.getDataSetList();
			if (dataSet != null)
			{
				int count = dataSet.size();		
				CNCronosLog.d("DataSet.size():" + count);
				
				if(mTotalPage == 0){
					mTotalPage = resultSet.getTotalCount() / PAGE_SIZE;
					if(resultSet.getTotalCount()%PAGE_SIZE != 0){
						mTotalPage+=1;
					}
				}
				for (int i = 0; i < count; i++)
				{
					CNLottoAdListData data = (CNLottoAdListData) dataSet.get(i);
					
					mDataSetList.add(data);
					
					mOriginImageArr.add(data.adOrigin);
					/*####### Ben ########*/
					// 상세보기에서 전화를 하기위한 Array 생성
					mPhoneNumberArr.add(data.adPhoneNum);
				}
			}
		}
		
		/*####### Ben ########*/
		// 새로 만드는 꽉찬 화면 리스트..
		setListVisibility(mDataSetList.size());
		
		
		
		
		/*mListAdapter.recycle();
		mListAdapter.setDataSetList(mDataSetList);
		mListAdapter.notifyDataSetChanged();*/
		setListImage();
		mCountTv.setText(mPage + " / " + mTotalPage);
		
		updateUI(resultSet.getRcode());
		
		/*####### Ben ########*/
		// Test code to see parsed data
		if(!CNLottoAdApiSet.isLive()) {
			Log.d("BenTag", "####################################################");
			Log.d("BenTag", "data : " + mOriginImageArr.toString());
			Log.d("BenTag", "data 2 : " + mDataSetList.toString());
			Log.d("BenTag", "phone number array : " + mPhoneNumberArr.toString());
			Log.d("BenTag", "xmlObject : " + xmlObject.toString());
			Log.d("BenTag", "####################################################");
//			mDataSetList.get(0);
			Log.d("BenTag", "parsing finish");
		}
		
	}
	
	private void updateUI(String rcode){
		if("RET0000".equals(rcode)){ 
			mListLayout.setVisibility(View.VISIBLE);
//			mAdGrid.setVisibility(View.VISIBLE);
			mEmptyListTv.setVisibility(View.GONE);
			mPageLayout.setVisibility(View.VISIBLE);
		} else if("RET0002".equals(rcode)){ // 목록이 없을시
//			mAdGrid.setVisibility(View.GONE);
			mListLayout.setVisibility(View.GONE);
			String msg = "";
			String suffix = getResources().getString(R.string.adlist_no_list_suffix);
			
			if(mCurrentListOption == OPTION_PART) {
				msg = mAddress + suffix;
			} else if(mCurrentListOption == OPTION_JOB) {
				msg = mJob + suffix;
			} else if(mCurrentListOption == OPTION_LOCATION) {
				msg = "내 위치에 " + suffix;
			} else {
				msg = getResources().getString(R.string.no_ad_list_available);
			}
			
			mEmptyListTv.setText(msg);
			
			mEmptyListTv.setVisibility(View.VISIBLE);
			mPageLayout.setVisibility(View.GONE);
		}
	}

	@Override
	public String onGetAsyncRequestUrl() {
		/*UnbindHelper.unbindReferences(mAdGrid);
		mAdGrid.setAdapter(mListAdapter);*/
		CNCronosLog.d("onGetAsyncRequestUrl()");
		String url = null;
		
		CNLottoAdApiSetEx MnetGlobalApiSetEx = CNLottoAdApiSetEx.getInstance();
		
		/*####### Ben ########*/
		// 리스트 옵션 부여함. else 이후가 기존 리스트 요청 
		if(mCurrentListOption != 0) {
			switch(mCurrentListOption) {
				case OPTION_PART:
					url = MnetGlobalApiSetEx.getAdAllUrl(mContext, mPage, PAGE_SIZE, ApplicationUtils.getPhoneNumber(mContext), "C", null, mSido, mGugun, mDong);
					break;
				case OPTION_JOB:
					url = MnetGlobalApiSetEx.getAdAllUrl(mContext, mPage, PAGE_SIZE, ApplicationUtils.getPhoneNumber(mContext), "B", mJobCode, null, null, null);
					break;
					
				case OPTION_LOCATION:
					url = MnetGlobalApiSetEx.getMyListUrl(mContext, mPage, PAGE_SIZE, ApplicationUtils.getPhoneNumber(mContext));
					break;
			}
			
		} else {
			if(mCurrentIndex == 0)
				url = MnetGlobalApiSetEx.getAdAllUrl(mContext, mPage, PAGE_SIZE, null, null, null, null, null, null);
			else {
				url = MnetGlobalApiSetEx.setCallUrl(mContext, mAdSeq, ApplicationUtils.getPhoneNumber(mContext));
			}
		}
		
		CNCronosLog.d("onGetAsyncRequestUrl", "url : "+url);
		
		return url;
	}
	
	@Override
	public void onAsyncCancelRequest() 
	{
		super.hideLoadingDialog();
		CNCronosLog.d("onAsyncCancelRequest()");
	}

	@Override
	public void onAsyncDataError(Element xmlObject, String errorMessage)
	{
		super.hideLoadingDialog();
		
		NodeList rcodeNode = xmlObject.getElementsByTagName("rcode");
		String rcode = rcodeNode.item(0).getFirstChild().getNodeValue();
		
		if(!"RET0002".equals(rcode)){
			ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.popup_alert), errorMessage);
		} else {
			updateUI(rcode);
		}
	}

	@Override
	public void onAsyncPreRequest()
	{
		super.showLoadingDialog();
	}

	@Override
	public void onAsyncRequestCompleted(Element xmlObject)
	{
		super.hideLoadingDialog();
			
		try {
			if(mCurrentIndex == 0)
				parsingAdList(xmlObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		showPagenumDuringGivenTime(3000);
	}

	@Override
	public void onAsyncRequestError(int errorNumber) {
		super.hideLoadingDialog();
		
		ApplicationUtils.showOkDialog(mContext,0,getString(R.string.popup_alert), ApplicationUtils.getErrorMessage(mContext, errorNumber));
		return;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		/*if(mApplication.isTabClicked()) {
			mApplication.setIsTabClicked(false);
			mPage = 1;
			mTotalPage = 0;
			mCurrentListOption = 0;
		}*/
		
		//전화걸기 화면에서 되돌아 왔을시에 현재 인덱스를 초기화 하고 url호출
		mCurrentIndex = 0;
		onRequestData();
		
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		System.gc();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		UnbindHelper.unbindReferences(getWindow().getDecorView().getRootView()); // 사용한 뷰를 메모리에서 제거 한다.
		System.gc(); // 가비지컬렉션 수행
	}
}
